'use strict';

const Controller = require('egg').Controller;

class SlideController extends Controller {
    
    // 列表
    async index() {
        const { ctx, app } = this;
        const { page, size } = ctx.query;

        let data = await ctx.page('Slide', {}, {
            order: [['order', 'ASC']]
        })
        data = JSON.parse(JSON.stringify(data))
        console.log(data)

        await ctx.renderTemplate({
            title: "轮播图列表",
            tempType: "table",
            table: {
                // 按钮
                buttons: {
                    // 新增操作
                    add: "/admin/slide/create"
                },
                // 表头
                columns: [{
                    title: "ID",
                    fixed: 'center',
                    key: "id"
                }, {
                    title: "标题",
                    fixed: 'left',
                    key: "title"
                }, {
                    title: "图片",
                    fixed: 'center',
                    render(item) {
                        let image = item.images ? item.images : '/public/assets/img/profiles/avatar-03.jpg'
                        return `
                        <h2 class="table-avatar">
                            <div class="avatar avatar-sm mr-2"><img class="avatar-img rounded" src="${image}"></div>
                        </h2>
                        `
                    }
                },{
                    title: "链接",
                    fixed: 'left',
                    key: "link"
                },{
                    title: "排序",
                    fixed: 'center',
                    key: "order"
                }, {
                    title: "状态",
                    fixed: 'center',
                    width: 180,
                    render(item) {
                        return `
                        <h2>
                            ${item.status === 0 ? "禁用" : "启用"}
                        </h2>
                        `
                    }
                }, {
                    title: "创建时间",
                    fixed: 'center',
                    width: 180,
                    key: "created_time"
                }, {
                    title: "操作",
                    width: 200,
                    fixed: 'center',
                    action: {
                        edit: function (id) {
                            return `/admin/slide/edit/${id}`
                        },
                        delete: function (id) {
                            return `/admin/slide/delete/${id}`
                        },
                    }
                }]
            },
            data
        })
    }
    // 创建轮播图表单
    async create() {
        const { ctx,app } = this;
        await ctx.renderTemplate({
            // 页面标题
            title: "添加轮播图",
            // 模板类型 form表单，table表格分页
            tempType: "form",
            // 表单配置
            form: {
                // 提交地址
                action: "/admin/slide",
                // 字段配置
                fields: [{
                    label: "标题",
                    type: "text",
                    name: "title",
                    placeholder: "标题"
                }, {
                    label: "轮播图",
                    type: "file",
                    name: "images",
                    placeholder: "轮播图"
                }, {
                    label: "链接",
                    type: "text",
                    name: "link",
                    placeholder: "链接"
                }, {
                    label: "排序",
                    type: "text",
                    name: "order",
                    placeholder: "排序"
                }, {
                    label: "状态",
                    type: "switch",
                    name: "status",
                    placeholder: "状态"
                }]
            },
            // 新增成功跳转路径
            successUrl: "/admin/slide"
        });
    }

    // 创建轮播图逻辑
    async save() {
        const { ctx, app } = this;

        ctx.validate({
            title: {
                type: "string",
                required: true,
                desc: "标题"
            },
            images: {
                type: "string",
                required: true,
                desc: "轮播图"
            },
            link: {
                type: "string",
                required: false,
                desc: "链接"
            },
            order: {
                type: "string",
                required: false,
                desc: "排序"
            },
            status: {
                type: "string",
                required: false,
                desc: "状态"
            }
        })

        let { title, images, link, order, status, created_time } = ctx.request.body;

        if (await app.model.Slide.findOne({
            where: {
                title
            }
        })) {
            return ctx.apiFail('该轮播图已存在');
        }

        let slide = await app.model.Slide.create({
            title, images, link, order, status, created_time
        })

        ctx.apiSuccess(slide)
    }

    // 编辑表单页
    async edit() {
        const { ctx, app } = this
        const id = ctx.params.id

        let data = await app.model.Slide.findByPk(id);
        if (!data) {
            return await ctx.pageFail('该记录不存在')
        }

        data = JSON.parse(JSON.stringify(data))
        await ctx.renderTemplate({
            id,
            title: "修改轮播图",
            tempType: "form",
            form: {
                // 提交地址
                action: "/admin/slide/" + id,
                // 字段配置
                fields: [{
                    label: "标题",
                    type: "text",
                    name: "title",
                    placeholder: "标题"
                }, {
                    label: "轮播图",
                    type: "file",
                    name: "images",
                    placeholder: "轮播图"
                }, {
                    label: "链接",
                    type: "text",
                    name: "link",
                    placeholder: "链接"
                }, {
                    label: "排序",
                    type: "text",
                    name: "order",
                    placeholder: "排序"
                }, {
                    label: "状态",
                    type: "switch",
                    name: "status",
                    placeholder: "状态"
                }],
                // 默认值
                data
            },
            // 修改成功跳转路径
            successUrl: "/admin/slide"
        })
    }

    // 更新逻辑
    async update() {
        const { ctx,app } = this;
        ctx.validate({
            id:{
                type:"int",
                required:true
            },
            title: {
                type: "string",
                required: true,
                desc: "标题"
            },
            images: {
                type: "string",
                required: true,
                desc: "轮播图"
            },
            link: {
                type: "string",
                required: false,
                desc: "链接"
            },
            order: {
                type: "string",
                required: false,
                desc: "排序"
            },
            status: {
                type: "string",
                required: false,
                desc: "状态"
            },
        })

        let id = ctx.params.id
        let { title, images, link, order, status } = ctx.request.body
        let slide = await app.model.Slide.findOne({
            where: {
                id
            }
        })
        if (!slide) {
            return ctx.apiFail('该记录不存在')
        }

        const Op = app.Sequelize.Op

        slide.title = title
        slide.images = images
        slide.link = link
        slide.order = order
        slide.status = status

        ctx.apiSuccess(await slide.save())

    }

    // 删除
    async delete() {
        const { ctx, app } = this;
        const id = ctx.params.id
        await app.model.Slide.destroy({
            where: {
                id
            }
        })
        ctx.toast('删除成功', 'success')
        ctx.redirect('/admin/slide')
    }
}

module.exports = SlideController;
